package com.tedu.openmedical.duplicate.basic_info.service.impl;

import com.tedu.openmedical.duplicate.basic_info.mapper.BasicInfoMapper;
import com.tedu.openmedical.duplicate.basic_info.pojo.dto.CopySettingsParam;
import com.tedu.openmedical.duplicate.basic_info.pojo.dto.InfoParam;
import com.tedu.openmedical.duplicate.basic_info.pojo.entity.History;
import com.tedu.openmedical.duplicate.basic_info.pojo.entity.HospitalInfo;
import com.tedu.openmedical.duplicate.basic_info.pojo.entity.MailInfo;
import com.tedu.openmedical.duplicate.basic_info.pojo.vo.CopySettingsVO;
import com.tedu.openmedical.duplicate.basic_info.pojo.vo.HistoryVO;
import com.tedu.openmedical.duplicate.basic_info.pojo.vo.HospitalInfoVO;
import com.tedu.openmedical.duplicate.basic_info.pojo.vo.MailInfoVO;
import com.tedu.openmedical.duplicate.basic_info.service.BasicInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BasicInfoServiceImpl implements BasicInfoService {
    private static final Logger log = LoggerFactory.getLogger(BasicInfoServiceImpl.class);
    @Autowired
    private BasicInfoMapper basicInfoMapper;

    @Override
    public HospitalInfoVO getHosInfo(){
        return basicInfoMapper.getHosInfo();
    }
    @Override
    public MailInfoVO getMailInfo(){ return basicInfoMapper.getMailInfo(); }
    @Override
    public List<HistoryVO> getHosInfoHistory(){
        List<History> hosInfoHis=basicInfoMapper.getHosInfoHistory();
        List<HistoryVO> list=new ArrayList<>();
        for(History item:hosInfoHis) {
            HistoryVO historyVO = setHistory(item);
            list.add(historyVO);
        }
        return list;
    }
    @Override
    public List<HistoryVO> getMailInfoHistory(){
        List<History> mailInfoHis=basicInfoMapper.getMailInfoHistory();
        List<HistoryVO> list=new ArrayList<>();
        for(History item:mailInfoHis) {
            HistoryVO historyVO = setHistory(item);
            list.add(historyVO);
        }
        return list;
    }
    @Override
    public void updateHosInfo(InfoParam infoParam){
        //操作：先插入一条修改记录，再改表
        //比对一下哪些记录是改了的
        History history=newHistory(infoParam,1);
        //插入履历
        if(history!=null) {
            basicInfoMapper.insertHosHistory(history);
            //修改信息
            HospitalInfo newInfo = new HospitalInfo();
            BeanUtils.copyProperties(infoParam, newInfo);
            basicInfoMapper.updateHosInfo(newInfo);
        }
    }
    @Override
    public void updateMailInfo(InfoParam infoParam){
        History history=newHistory(infoParam,2);
        if(history!=null) {
            basicInfoMapper.insertMailHistory(history);
            //修改信息
            MailInfo newInfo = new MailInfo();
            BeanUtils.copyProperties(infoParam, newInfo);
            basicInfoMapper.updateMailInfo(newInfo);
        }
    }
    @Override
    public CopySettingsVO getCopySettings(){
        CopySettingsVO settings = basicInfoMapper.getCopySettings();
        return settings;
    }
    @Override
    public void updateCopySett(CopySettingsParam param){
        basicInfoMapper.updateCopySettings(param);
    }

    //对履历信息的处理
    private HistoryVO setHistory(History history){
        HistoryVO historyVO = new HistoryVO();
        //复制不需处理的数据
        historyVO.setId(history.getId());
        historyVO.setUpdateTime(history.getUpdateTime());
        historyVO.setUpdateUser(history.getUpdateUser());
        //处理三个List
        if(history.getParamName()!=null) historyVO.setParamName(history.getParamName().split(","));
        else historyVO.setParamName(new String[]{});
        if(history.getUpdateBefore()!=null) historyVO.setUpdateBefore(history.getUpdateBefore().split(","));
        else historyVO.setParamName(new String[]{});
        if(history.getUpdateAfter()!=null) historyVO.setUpdateAfter(history.getUpdateAfter().split(","));
        else historyVO.setParamName(new String[]{});
        return historyVO;
    }
    //对传来的修改信息的判断处理
    private History newHistory(InfoParam infoParam,int mode){
        //添加模式判定：1检查医院信息，2检查邮寄信息，原设计两表内容不一样
        HospitalInfoVO hosInfo = basicInfoMapper.getHosInfo();
        MailInfoVO mailInfo=basicInfoMapper.getMailInfo();
        List<String> paramName=new ArrayList<>();
        List<String> updateBefore=new ArrayList<>();
        List<String> updateAfter=new ArrayList<>();
        if(mode==1) {
            if (!infoParam.getName().equals(hosInfo.getName())) {
                //名字改了
                paramName.add("name");
                updateBefore.add(hosInfo.getName());
                updateAfter.add(infoParam.getName());
            }
            if (!infoParam.getPhone().equals(hosInfo.getPhone())) {
                //电话改了
                paramName.add("phone");
                updateBefore.add(hosInfo.getPhone());
                updateAfter.add(infoParam.getPhone());
            }
            if (!infoParam.getAddr().equals(hosInfo.getAddr())) {
                //地址改了
                paramName.add("addr");
                updateBefore.add(hosInfo.getAddr());
                updateAfter.add(infoParam.getAddr());
            }
        }
        else if(mode==2){
            if (!infoParam.getName().equals(mailInfo.getName())) {
                //名字改了
                paramName.add("name");
                updateBefore.add(mailInfo.getName());
                updateAfter.add(infoParam.getName());
            }
            if (!infoParam.getPhone().equals(mailInfo.getPhone())) {
                //电话改了
                paramName.add("phone");
                updateBefore.add(mailInfo.getPhone());
                updateAfter.add(infoParam.getPhone());
            }
            if (!infoParam.getAddr().equals(mailInfo.getAddr())) {
                //地址改了
                paramName.add("addr");
                updateBefore.add(mailInfo.getAddr());
                updateAfter.add(infoParam.getAddr());
            }
        }
        if(!paramName.isEmpty()) {
            History history = new History();
            history.setParamName(transString(paramName));
            history.setUpdateBefore(transString(updateBefore));
            history.setUpdateAfter(transString(updateAfter));
            history.setUpdateTime(new Date());
            log.debug(history.toString());
            return history;
        }
        return null;
    }
    //存储字段格式处理
    private String transString(List<String> list){
        StringBuilder newString=new StringBuilder();
        for(String s:list){
            newString.append(s);
            newString.append(",");
        }
        newString.deleteCharAt(newString.length()-1);
        String string = newString.toString();
        return string;
    }
}
